package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-sys/cache"
	"logi-sys/models"
	"strconv"
	"time"
)

func WarehouseAdd(c *gin.Context) {
	warehouse_code := c.PostForm("warehouse_code")
	warehouse_name := c.PostForm("warehouse_name")
	warehouse_address := c.PostForm("warehouse_address")

	data := models.Warehouse{
		WarehouseCode:    warehouse_code,
		WarehouseName:    warehouse_name,
		WarehouseAddress: warehouse_address,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_address := c.PostForm("order_address")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	wid, _ := strconv.Atoi(c.PostForm("wid"))

	data := models.Order{
		OrderNo:      order_no,
		OrderAddress: order_address,
		OrderNum:     order_num,
		OrderPrice:   order_price,
		Wid:          wid,
	}
	models.DB.Create(&data)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	Offset := (page - 1) * size

	var orderList []models.OrderList

	/*
		SELECT
		  warehouses.warehouse_code as WarehouseCode,
		  warehouses.warehouse_name as WarehouseName,
		  warehouses.warehouse_address as WarehouseAddress,
		  orders.order_no as OrderNo,
		  orders.order_address as OrderAddress,
		  orders.order_num as OrderNum,
		  orders.order_price as OrderPrice
		FROM
		  warehouses
		  LEFT JOIN orders ON warehouses.id = orders.wid
	*/

	redis_key := "order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"warehouses.warehouse_code as WarehouseCode",
				"warehouses.warehouse_name as WarehouseName",
				"warehouses.warehouse_address as WarehouseAddress",
				"orders.order_no as OrderNo",
				"orders.order_address as OrderAddress",
				"orders.order_num as OrderNum",
				"orders.order_price as OrderPrice").
			Table("warehouses").
			Joins("LEFT JOIN orders ON warehouses.id = orders.wid").
			Offset(Offset).Limit(size).Debug().Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	var total int64
	models.DB.Model(&models.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  orderList,
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
	 SELECT
	   warehouses.warehouse_code as WarehouseCode,
	   warehouses.warehouse_name as WarehouseName,
	   warehouses.warehouse_address as WarehouseAddress,
	   orders.order_no as OrderNo,
	   orders.order_address as OrderAddress,
	   orders.order_num as OrderNum,
	   orders.order_price as OrderPrice
	 FROM
	   warehouses
	   LEFT JOIN orders ON warehouses.id = orders.wid
	 WHERE orders.id=1
	*/
	var orderList models.OrderList

	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Select(
				"warehouses.warehouse_code as WarehouseCode",
				"warehouses.warehouse_name as WarehouseName",
				"warehouses.warehouse_address as WarehouseAddress",
				"orders.order_no as OrderNo",
				"orders.order_address as OrderAddress",
				"orders.order_num as OrderNum",
				"orders.order_price as OrderPrice").
			Table("warehouses").
			Joins("LEFT JOIN orders ON warehouses.id = orders.wid").
			Where("orders.id=?", id).
			Debug().Find(&orderList)

		jsonData, _ := json.Marshal(orderList)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orderList)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": orderList,
	})
	return
}
